package boun.swe.bob.service;

import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ememory
 * Date: 27.Ara.2010
 * Time: 18:01:12
 * To change this template use File | Settings | File Templates.
 */

public class AddGroupController extends BaseController {
    @RequestMapping(method = RequestMethod.GET)
        public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();
        myModel.put("himself", true);
           myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
            return new ModelAndView("addGroup", "model", myModel);
        }

    @RequestMapping(method = RequestMethod.POST)
        public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();

            String resultText="Can't create the group!";
            try {
            String groupName = httpServletRequest.getParameter("groupName");
            String groupDescription = httpServletRequest.getParameter("groupDescription");

            compositeDao.AddGroup(getLoggedInUser(httpServletRequest).getUserId(),groupName,groupDescription);
            resultText="Group Created!";
            }
            catch (Exception exp) {
            }

            myModel.put("resultText",resultText);
        myModel.put("himself", true);
           myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
            return new ModelAndView("addGroup", "model", myModel);

        }

}